//index.js
//获取应用实例
const common = require('../../utils/util.js')
const app = getApp()

Page({
    data: {
        windowHeight: 0,
        nowDate: null,
        shopInfo: null,
        userInfo: null,
        userCode: '',
        userType: null,
        belongRefer: null,
        showAdv: true,
        popImg: '',
        relayWord: '',
        relayImg: '',
        entranceImg: '',
        homeAdv_1: '',
        fixAdvShow: true,
        showShareImg: false,
        shareImg: [],
        previewImg: '',
        imgSwiper: [],
        shopExclusiveGoods: [],
        upgradeGoods: [],
        newGoods: [],
        hotSaleGoods: [],
        hotClickGoods: [],
        zeroBuy: [],
        zeroBuyList: [],
        indexTypeClass: [],
        indexClassList: [],
        guessLikeClass: [],
        guessLikeList: [],
        guessLikeIndex: -1,
        nowPage: 0,
        isLoading: false,
        isAll: false,
        shopGoodsClassify: [],
        shopGoodsClassifyIndex: 'all',
        shopGoodsList: [],
        shopActivityGoods: [],
        shopIntroGoods: [],
        shopVideo: [],
        vipGoods: [],
        agentGoods: [],
        shopGrow: null
    },
    onLoad: function(options) {
        if (options.scene) {
            let sceneArr = options.scene.split(',')
            wx.setStorageSync('userShare', sceneArr[0])
            if (wx.getStorageSync('token')) {
                let data = {
                    belong_refer: sceneArr[0]
                }
                common.post('User/set_belong_refer', data).then(function(res) {}).catch(function(e) {})
            }
        }
        // if (options.storeId) {
        //     wx.setStorageSync('storeId', options.storeId)
        //     if (wx.getStorageSync('token')) {
        //         let data = {
        //             store_id: options.storeId
        //         }
        //         common.post("Store/change_user_store", data).then(function(res) {}).catch(function(e) {})
        //     }
        // }
        if (options.userShare) {
            wx.setStorageSync('userShare', options.userShare)
            if (wx.getStorageSync('token')) {
                let data = {
                    belong_refer: options.userShare
                }
                common.post('User/set_belong_refer', data).then(function(res) {}).catch(function(e) {})
            }
        }
        if (wx.getStorageSync('roleType')) {
            this.setData({
                userType: wx.getStorageSync('roleType')
            })
        }
        this.setData({
            nowDate: new Date().getTime()
        })
        const that = this
        wx.getSystemInfo({
            success: function(res) {
                // 获取可使用窗口宽度
                let clientHeight = res.windowHeight;
                // 获取可使用窗口高度
                let clientWidth = res.windowWidth;
                // 算出比例
                let ratio = 750 / clientWidth;
                // 算出高度(单位rpx)
                let height = clientHeight * ratio;
                // 设置高度
                that.setData({
                    windowHeight: clientHeight
                })
            }
        })
        if (wx.getStorageSync('token')) {
            this.getUserInfo()
        }
        this.getOnOff()
        this.getRelayInfo()
        this.getShopInfo()
        this.getBannerImg()
        this.getPopImg()
        this.getEntranceImg()
        this.getHomeAdv_1()
        this.getIndexData()
        // this.getShopExclusiveGoods()
        this.getNewGoods()
        // this.getHotSaleGoods()
        // this.getHotClickGoods()
        this.getZeroBuy()
        this.getShareImg()
        // this.getShopGoodsClassify()
        // this.getShopGoodsList()
        // this.getShopActivityGoods()
        // this.getShopIntroGoods()
        this.getShopVideo()
        // this.getVipGoods()
        this.getAgentGoods()
    },
    onHide: function() {
        this.setData({
            isLoading: false
        })
    },
    onShow: function() {

    },
    onPullDownRefresh: function() {
        this.setData({
            windowHeight: 0,
            nowDate: null,
            shopInfo: null,
            userInfo: null,
            userCode: '',
            userType: null,
            belongRefer: null,
            popImg: '',
            realyImg: '',
            entranceImg: '',
            homeAdv_1: '',
            fixAdvShow: true,
            showShareImg: false,
            shareImg: [],
            previewImg: '',
            imgSwiper: [],
            upgradeGoods: [],
            newGoods: [],
            hotSaleGoods: [],
            hotClickGoods: [],
            zeroBuy: [],
            zeroBuyList: [],
            indexTypeClass: [],
            indexClassList: [],
            guessLikeClass: [],
            guessLikeList: [],
            guessLikeIndex: -1,
            nowPage: 0,
            isLoading: false,
            isAll: false,
            shopGoodsClassify: [],
            shopGoodsClassifyIndex: 'all',
            shopGoodsList: [],
            shopActivityGoods: [],
            shopIntroGoods: [],
            shopVideo: [],
            vipGoods: [],
            agentGoods: [],
            shopGrow: null
        })
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 1000)
    },

    onReachBottom: function() {
        this.getGuessLikeList()
    },

    onPageScroll: function(e) {
        // let query = wx.createSelectorQuery()
        // query.select('#memberService').boundingClientRect((rect) => {
        //     let top = rect.top
        //     if (top < this.data.windowHeight) {
        //         this.setData({
        //             fixAdvShow: false
        //         })
        //     } else {
        //         this.setData({
        //             fixAdvShow: true
        //         })
        //     }
        // }).exec()
    },

    onShareAppMessage: function(res) {
        let userId = wx.getStorageSync('userId')
        let storeId = wx.getStorageSync('storeId')
        if (userId) {
            return {
                title: this.data.relayWord,
                path: '/pages/index/index?userShare=' + userId,
                imageUrl: this.data.relayImg
            }
        }
    },

    closeAdv: function() {
        this.setData({
            showAdv: false
        })
        wx.showTabBar()
    },

    navClick: function(e) {
        if (e.detail == this.data.shopGoodsClassifyIndex) {
            return
        }
        this.setData({
            shopGoodsClassifyIndex: e.detail,
            shopGoodsList: []
        })
        this.getShopGoodsList()
    },

    getOnOff: function() {
        const that = this
        common.post('Home/index', {
            activity: 785
        }).then(function(res) {
            if (res.code == 204) {
                return
            }
            if (res.show_video == app.globalData.videoShow) {
                that.setData({
                    videoShow: true
                })
            } else {
                that.setData({
                    videoShow: false
                })
            }
        }).catch(function(e) {})
    },

    getRelayInfo: function() {
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let data = {
            activity: 785,
            store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 2
        }
        common.post('Store/share_images', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                return
            }
            that.setData({
                relayWord: res.share_txt,
                relayImg: res.share_images
            })
        }).catch(function(e) {})
    },

    getShopExclusiveGoods: function() {
        const that = this
        let data = {
            activity: 785,
            store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 2,
            stauts: '1',
            limit: 999,
            page: 0
        }
        common.post('Storegoods/store_goods_list', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                shopExclusiveGoods: res
            })
        }).catch(function(e) {})
    },

    getShopGoodsClassify: function() {
        const that = this
        let data = {
            activity: 785,
            store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 2,
            page: 0,
            limit: 999
        }
        common.post('Goodsclass/store_class_list', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                shopGoodsClassify: res
            })
        }).catch(function(e) {})
    },

    getShopGoodsList: function() {
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let data
        if (this.data.shopGoodsClassifyIndex == 'all') {
            data = {
                activity: 785,
                store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 2,
                is_sale: '1',
                is_goods: '0',
                page: 0,
                limit: 6,
                client: 2
            }
        } else {
            data = {
                activity: 785,
                store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 2,
                class_id: this.data.shopGoodsClassify[this.data.shopGoodsClassifyIndex].class_id,
                is_sale: '1',
                is_goods: '0',
                page: 0,
                limit: 6,
                client: 2
            }
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                return
            }
            that.setData({
                shopGoodsList: res
            })
        }).catch(function(e) {})
    },

    getShopActivityGoods: function() {
        const that = this
        let data = {
            activity: 785,
            store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 2,
            is_sale: '1',
            is_goods: '0',
            is_actvity_goods: '1',
            page: 0,
            limit: 99,
            client: 2
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                return
            }
            that.setData({
                shopActivityGoods: res
            })
        }).catch(function(e) {})
    },

    getShopIntroGoods: function() {
        const that = this
        let data = {
            activity: 785,
            store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 2,
            is_sale: '1',
            is_goods: '0',
            recommend: '1',
            page: 0,
            limit: 6,
            client: 2
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                return
            }
            that.setData({
                shopIntroGoods: res
            })
        }).catch(function(e) {})
    },

    getShopVideo: function() {
        const that = this
        let data = {
            activity: 785,
            store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 2,
            page: 0,
            limit: 6
        }
        common.post('Video/video_list', data).then(function(res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    shopVideo: res
                })
            }
        }).catch(function(e) {})
    },

    getVipGoods: function() {
        const that = this
        let data = {
            activity: 785,
            store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 2,
            is_sale: '1',
            is_goods: '1',
            page: 0,
            limit: 4,
            client: 2
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                vipGoods: res
            })
        }).catch(function(e) {})
    },

    getAgentGoods: function() {
        const that = this
        let data = {
            activity: 785,
            store_id: 2,
            is_sale: '1',
            is_goods: '1',
            agency_goods: '1',
            page: 0,
            limit: 1,
            client: 2
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                agentGoods: res
            })
        }).catch(function(e) {})
    },

    getShopInfo: function() {
        const that = this
        let data = {
            activity: 785,
            store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 2
        }
        common.post('Store/get_store_info', data).then(function(res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    shopInfo: res
                })
                wx.setNavigationBarTitle({
                    title: res.store_name
                })
            }
        }).catch(function(e) {})
    },

    getShopGrow: function() {
        const that = this
        let data = {
            store_id: wx.getStorageSync('realStoreId')
        }
        common.post('Store/store_data', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                shopGrow: res
            })
        }).catch(function(e) {})
    },

    getUserInfo: function() {
        const that = this
        let data = {}
        common.post('User/user_info', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                userInfo: res
            })
            if (res.have_shop == 1) {
                that.getShopGrow()
            }
        }).catch(function(e) {})
    },

    getUserCode: function() {
        const that = this
        let data = {
            store_id: wx.getStorageSync('storeId')
        }
        common.post('QrCodes/app_create_qr', data).then(function(res) {
            that.setData({
                userCode: res
            })
        }).catch(function(e) {})
    },

    getShareImg: function() {
        let that = this;
        common.post("Upimg/get_app_img", {
            activity: 785,
            type: 1
        }).then(function(res) {
            that.setData({
                shareImg: res,
                previewImg: res[0]
            })
        }).catch(function(e) {})
    },

    getPopImg: function() {
        const that = this
        let data = {
            activity: 785,
            images_id: 38
        }
        common.post('Images/get_img', data).then(function(res) {
            that.setData({
                popImg: res[0]
            })
        }).catch(function(e) {})
    },

    getEntranceImg: function() {
        const that = this
        let data = {
            activity: 785,
            images_id: 12
        }
        common.post('Images/get_img', data).then(function(res) {
            that.setData({
                entranceImg: res[0].images
            })
        }).catch(function(e) {})
    },

    getHomeAdv_1: function() {
        const that = this
        let data = {
            activity: 785,
            images_id: 17
        }
        common.post('Images/get_img', data).then(function(res) {
            that.setData({
                homeAdv_1: res[0].images
            })
        }).catch(function(e) {})
    },

    getBannerImg: function() {
        const that = this
        let data = {
            activity: 785,
            store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 2,
            is_online: 1,
            client: 2,
            page: 0,
            limit: 99
        }
        common.post('Slideshow/slideshow_list', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    imgSwiper: []
                })
            } else {
                that.setData({
                    imgSwiper: res
                })
            }
        }).catch(function(e) {})
    },

    getIndexData: function() {
        const that = this
        let data = {
            activity: 785,
        }
        common.post('Home/index', data).then(function(res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    indexTypeClass: res.recommen_class,
                    guessLikeClass: res.all_class
                })
                that.getClassList()
                that.getGuessLikeList()
            }
        }).catch(function(e) {})
    },

    getClassList: function() {
        const that = this
        let indexClassList = []
        common.post('shelf/get_shelf', {
            activity: 785
        }).then(function(res) {
            if (res.code == 204) {
                indexClassList = []
            } else {
                indexClassList = res
            }
            that.setData({
                indexClassList: indexClassList
            })
        }).catch(function(e) {})
    },

    getGuessLikeList: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        const that = this
        let guessLikeList = this.data.guessLikeList
        let data
        if (this.data.guessLikeIndex == -1) {
            data = {
                activity: 785,
                is_goods: "0",
                page: this.data.nowPage,
                limit: 10,
                client: 1
            }
        } else {
            data = {
                activity: 785,
                is_goods: "0",
                page: this.data.nowPage,
                limit: 10,
                class_id: this.data.guessLikeClass[this.data.guessLikeIndex].class_id,
                client: 1
            }
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    guessLikeList: guessLikeList,
                    isAll: true
                })
            } else {
                guessLikeList = guessLikeList.concat(res)
                that.setData({
                    guessLikeList: guessLikeList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    },

    getUpgradeGoods: function() {
        const that = this
        let data = {
            activity: 785,
            type: 3
        }
        common.post('shelf/get_shelf', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    upgradeGoods: []
                })
            } else {
                that.setData({
                    upgradeGoods: res
                })
            }
        }).catch(function(e) {})
    },

    getNewGoods: function() {
        const that = this
        let data = {
            activity: 785,
            order_by: 'add_time',
            asc: 2,
            page: 0,
            limit: 20,
            client: 1
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    newGoods: []
                })
            } else {
                that.setData({
                    newGoods: res
                })
            }
        }).catch(function(e) {})
    },

    getHotSaleGoods: function() {
        const that = this
        let data = {
            activity: 785,
            order_by: 'sold_num',
            asc: 2,
            page: 0,
            limit: 20,
            client: 1
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                hotSaleGoods: res
            })
        }).catch(function(e) {})
    },

    getHotClickGoods: function() {
        const that = this
        let data = {
            activity: 785,
            order_by: 'click',
            asc: 2,
            page: 0,
            limit: 20,
            client: 1
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                hotClickGoods: res
            })
        }).catch(function(e) {})
    },

    goShopVipGoods: function() {
        wx.navigateTo({
            url: '/pages/shopVipGoods/shopVipGoods'
        })
    },

    guessClick: function(e) {
        if (this.data.guessLikeIndex == e.detail) {
            return
        }
        this.setData({
            guessLikeIndex: e.detail,
            guessLikeList: [],
            nowPage: 0,
            isAll: false,
            isLoading: false
        })
        this.getGuessLikeList()
    },

    getZeroBuy: function() {
        const that = this
        let data = {
            activity: 785,
            type: 6
        }
        common.post('Shelf/get_shelf', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    zeroBuy: []
                })
            } else {
                let zeroBuyList = that.data.zeroBuyList
                that.setData({
                    zeroBuy: res
                })
                that.data.zeroBuy.forEach((item, i) => {
                    let data = {
                        activity: 785,
                        shelf_id: item.id
                    }
                    common.post('Activity/get_activity_goods', data).then(function(res) {
                        if (res.code == 204) {
                            zeroBuyList[i] = []
                        } else {
                            zeroBuyList[i] = res
                        }
                        that.setData({
                            zeroBuyList: zeroBuyList
                        })
                    }).catch(function(e) {})
                })
            }
        }).catch(function(e) {})
    },

    goTeach: function() {
        wx.navigateTo({
            url: '/pages/h5/h5?page=index_5'
        })
    },

    goMyShop: function() {
        wx.showLoading({
            title: '请稍后'
        })
        const that = this
        let data = {
            store_id: this.data.userInfo.real_store_id
        }
        common.post("Store/change_user_store", data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                return
            }
            wx.setStorageSync('storeId', that.data.userInfo.real_store_id)
            wx.reLaunch({
                url: '/pages/index/index'
            })
        }).catch(function(e) {})
    },

    goApplyAgent: function() {
        wx.navigateTo({
            url: '/pages/shopManager/shopBuy/shopBuy'
        })
    }
})